<?php

class Clients_model extends CI_Model {

    function  __construct() {
        parent::__construct();
        $this->tableName = 'set_clients';
        $this->load->model("functions_model");
        $this->load->model("consolidate_model");
    }

    function newClient($added_by) {
        $data = $_POST;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }


    function getAllMini($tid=NULL) {
        if ($tid) {
            $data['tid'] = $tid;
            $this->db->where($data);
        }
        $this->db->select('tid,name');
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        return $query->result_array();
    }

    function getAll($tid=NULL) {
        if ($tid) {
            $data['tid'] = $tid;
            $this->db->where($data);
        }
        $this->db->select('tid,added_by,name,link,vat_comp,list_of_dire, date_added');
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            unset($row['tid']);
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $row['list_of_dire'] = nl2br($row['list_of_dire']);
            $row['link'] = "<a href='" . $row['link'] . "'>" . $row['link'] . "</a>";
            $list = $this->consolidate_model->getYeses($row['vat_comp']);
            if (count($list) > 0) {
                $row['vat_comp'] = $list[0]->name;
            }
            $row['links'] = "<a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_settings/deleteClient/$tid\",\"Are you sure to delete this client?\")'>Delete</a>";
            $result[] = $row;
        }
        return $result;
    }

}

?>
